﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Antioch.Base;

namespace Antioch
{
    class ScreenGeneralMenu : Screen
    {
        int Place = 0;
		Texture2D Title;
		SpriteFont Font;
		public bool DrawTitle = true;

        public delegate void MenuChoice();
		public delegate void BackDelegate();

		public BackDelegate OnBackPressed = null;

		public override void Initialize()
		{
			Title = Content.Load<Texture2D>("Antioch.Content/title");
			Font = Content.Load<SpriteFont>("Antioch.Content/hud");
			base.Initialize();
		}

        struct MenuOption
        {
            public string Label;
            public MenuChoice OnChoosen;
        };

        List<MenuOption> MenuOptions = new List<MenuOption>();


        public void AddOption(string Label, MenuChoice Callback)
        {
            MenuOption NewOption = new MenuOption();
            NewOption.Label = Label;
            NewOption.OnChoosen = Callback;

            MenuOptions.Add(NewOption);

        }

        void DecPlace()
        {
            --Place;
            if (Place < 0) Place = MenuOptions.Count - 1;
        }

        void IncPlace()
        {
            ++Place;
            if (Place >= MenuOptions.Count) Place = 0;
        }

        public override void ButtonEvent(PlayerIndex index, EventBasedInput.Buttons button, EventBasedInput.EventTypes type, TimeSpan duration)
        {
            if (type != EventBasedInput.EventTypes.Pressed) return;
            if (index != MainController) return;

			if (button == EventBasedInput.Buttons.A)
			{
				if (MenuOptions[Place].OnChoosen != null) MenuOptions[Place].OnChoosen();
			}
			else if (button == EventBasedInput.Buttons.DPadUp)
				DecPlace();
			else if (button == EventBasedInput.Buttons.DPadDown)
				IncPlace();
			else if (button == EventBasedInput.Buttons.Back && OnBackPressed != null)
				OnBackPressed();
        }

		int StickMovements = 0;
		float StickRate = 0.3f;

		public override void StickEvent(PlayerIndex index,
			EventBasedInput.Sticks stick, 
			EventBasedInput.EventTypes type, 
			Vector2 delta, TimeSpan duration)
		{
			if (index != MainController) return;

			if (type == EventBasedInput.EventTypes.Pressed)
			{
				StickMovements = 0;
				if (delta.Y < 0) IncPlace();
				else DecPlace();
			}
			else
			{
				if (delta.Y < 0)
				{
					if (StickMovements > 0) StickMovements = 0;
					float TimeSoFar = -StickMovements * StickRate;
					if (duration.TotalSeconds - TimeSoFar > StickRate)
					{
						IncPlace();
						--StickMovements;
					}
				}
				else
				{
					if (StickMovements < 0) StickMovements = 0;
					float TimeSoFar = StickMovements * StickRate;
					if (duration.TotalSeconds - TimeSoFar > StickRate)
					{
						DecPlace();
						++StickMovements;
					}


				}
			}
		}

        public override void Draw()
        {
			SpriteBatch.Begin();

			if (DrawTitle)
			{
				int ScreenWidth = GraphicsDevice.Viewport.TitleSafeArea.Width;
				int ScreenHeight = GraphicsDevice.Viewport.TitleSafeArea.Height;
				Rectangle SpritePosition = new Rectangle();
				SpritePosition.X = GraphicsDevice.Viewport.TitleSafeArea.X + (ScreenWidth / 2) - ScreenHeight;
				SpritePosition.Width = ScreenHeight * 2;
				SpritePosition.Y = GraphicsDevice.Viewport.TitleSafeArea.Y;
				SpritePosition.Height = ScreenHeight;



				SpriteBatch.Draw(Title, SpritePosition, Color.White);
			}

			float DrawY = GraphicsDevice.Viewport.TitleSafeArea.Center.Y - (MenuOptions.Count * 24);

			for (int i = 0; i < MenuOptions.Count; ++i)
			{
				float DrawX = GraphicsDevice.Viewport.TitleSafeArea.Center.X - Font.MeasureString(MenuOptions[i].Label).X / 2;
				SpriteBatch.DrawString(Font, MenuOptions[i].Label, new Vector2(DrawX, DrawY),
					(i == Place ? Color.Red : Color.White));
				DrawY += 48;
			}

			SpriteBatch.End();
        }
    }
}
